/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figures 2 (B & C)

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/Fig2`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/AKM_Firm_FE_for_export_stata.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/

	rename obs total_obs

	** Figure 2.A

    *Winsorize (trim) Firm Fixed Effects
    winsor2 firm_fe4, replace cuts(1 99) trim
    winsor2 uf_fe4, replace cuts(1 99) trim

    *Normalization
    qui sum firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & uf_fe4 == . & connected_set == 1
    local mean_reg = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg'

    *Compute Means to report in Figure
    sum firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & connected_set == 1 & uf_fe4 != 0 & uf_fe4 != .
    local mean_temp = r(mean)
    sum firm_fe4 [aw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & uf_fe4 == . & connected_set == 1
    local mean_reg = r(mean)
	global dif_mean = `mean_reg' - `mean_temp'
	display("The difference of means is: ${dif_mean}")

    *Plot Histogram 2.B
    twoway (histogram firm_fe4 [fw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & connected_set == 1 & uf_fe4 != 0 & uf_fe4 != ., fraction width(.05) color(gs7)) ///
           (histogram firm_fe4 [fw=total_obs] if firm_fe4 != 0 & firm_fe4 != . & uf_fe4 == . & connected_set == 1, fraction width(.05) fcolor(none) lcolor(black)) ///
           (scatteri 0 `mean_temp' .113 `mean_temp', c(l) m(i) lcolor(black) lwidth(thick)) ///
           (scatteri 0 `mean_reg' .113 `mean_reg', c(l) m(i) lwidth(thick) lcolor(black) lpattern(dash)), legend(cols(1)order(1 "Ever User Firms" 2 "Never User Firms" 3 "Mean Ever User Firms" 4 "Mean Never User Firms")position(6)region(color(white))) ysc(r(0 0.12)) ylabel(0(.03)0.12) graphregion(color(white)) plotregion(color(white)) xtitle("Firm FE", size(medlarge)) xsize(16) ysize(9) text(0.12 .14 "{&Delta} = 0.27" "{&larr}{&rarr}")
    graph export "${Results}/Histogram_UF_NONUF_akm4_FirmFE.pdf", replace

    ** Figure 2.B

    *Winsorize (trim) Firm Fixed Effects
    winsor2 firm_fe3, replace cuts(1 99) trim
    winsor2 uf_fe3, replace cuts(1 99) trim

    *Normalization
    qui sum firm_fe3 [aw=total_obs] if firm_fe3 != 0 & firm_fe3 != . & connected_set == 1 & uf_fe3 != 0 & uf_fe3 != .
    local mean_reg = r(mean)
    replace firm_fe3 = firm_fe3 - `mean_reg'
    replace uf_fe3 = uf_fe3 - `mean_reg'

    *Compute Means to report in Figure
    sum uf_fe3 [aw=total_obs] if uf_fe3 != 0 & uf_fe3 != . & connected_set == 1 & firm_fe3 != 0 & firm_fe3 != .
    local mean_temp = r(mean)
    sum firm_fe3 [aw=total_obs] if firm_fe3 != 0 & firm_fe3 != . & connected_set == 1 & uf_fe3 != 0 & uf_fe3 != .
    local mean_reg = r(mean)
    global dif_mean = `mean_reg' - `mean_temp'
    display("The difference of means is: ${dif_mean}")

	*Now compute the difference of means weighting by number of temp workers
    sum uf_fe3 [aw=obs_temp] if uf_fe3 != 0 & uf_fe3 != . & connected_set == 1 & firm_fe3 != 0 & firm_fe3 != .
    local mean_temp = r(mean)
    sum firm_fe3 [aw=obs_temp] if firm_fe3 != 0 & firm_fe3 != . & connected_set == 1 & uf_fe3 != 0 & uf_fe3 != .
    local mean_reg = r(mean)
	global dif_mean = `mean_reg' - `mean_temp'
	display("The difference of means is: ${dif_mean}")

    *Plot Histogram 2.C
    twoway (histogram uf_fe3 [fw=total_obs] if uf_fe3 != 0 & uf_fe3 != . & connected_set == 1 & firm_fe3 != 0 & firm_fe3 != ., width(.05) fraction color(gs7)) ///
           (histogram firm_fe3 [fw=total_obs] if firm_fe3 != 0 & firm_fe3 != . & connected_set == 1 & uf_fe3 != 0 & uf_fe3 != ., width(.05) fraction fcolor(none) lcolor(black)) ///
           (scatteri 0 `mean_temp' .14 `mean_temp', c(l) m(i) lcolor(black) lwidth(thick)) ///
           (scatteri 0 `mean_reg' .14 `mean_reg', c(l) m(i) lwidth(thick) lcolor(black) lpattern(dash)), legend(cols(1)order(1 "Firm FE for Temp Workers" 2 "Firm FE for Regular Workers" 3 "Mean Firm FE for Temp Workers" 4 "Mean Firm FE for Regular Workers")position(6)region(color(white))) graphregion(color(white)) plotregion(color(white)) ysc(r(0 .16)) ylabel(0(.04).16) xtitle("Firm FE", size(medlarge)) xsize(16) ysize(9) text(0.15 0 "{&Delta} = 0.17" "{&larr}{&rarr}", placement(w))
    graph export "${Results}/Histogram_reg_temp_akm3_SameWorkplace_FirmFE.pdf", replace

log close
